package com.mystyle.le.weekproject.utils;

import android.util.Log;

import com.mystyle.le.weekproject.beans.Boutiques;
import com.mystyle.le.weekproject.beans.DetialList;
import com.mystyle.le.weekproject.beans.HeadList;
import com.mystyle.le.weekproject.beans.HeadTitls;
import com.mystyle.le.weekproject.beans.Manchester;
import com.mystyle.le.weekproject.beans.PassList;
import com.mystyle.le.weekproject.beans.ReaderPicture;
import com.mystyle.le.weekproject.beans.Seniority;
import com.mystyle.le.weekproject.beans.TalkList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by le on 2016/10/9.
 */
public class ParseJson {
    /*
    * 解析精品页面顶部广告数据
    * */
    public static List<Manchester> parseJson(String jsonPath){
        List<Manchester> manchesterList = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(jsonPath);
            if (jsonObject!=null){
                JSONArray carousel_figure = jsonObject.getJSONArray("carousel_figure");
                for (int i =0;i<carousel_figure.length();i++){
                    JSONObject jsonObject1 = carousel_figure.getJSONObject(i);
                    String id = jsonObject1.getString("id");
                    String name = jsonObject1.getString("name");
                    String icon = jsonObject1.getString("icon");
                    String cover = jsonObject1.getString("cover");
                    manchesterList.add(new Manchester(id,name,icon,cover));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return manchesterList;
    }

    /*
    * 解析排行页面数据
    * */
    public static List<Seniority> parseSeniority(String pathJson){
        List<Seniority> seniorityList = new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONArray(pathJson);
            if (jsonArray!=null&&jsonArray.length()>0){
                for (int i=0;i<jsonArray.length();i++){
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String id = jsonObject.getString("id");
                    String name = jsonObject.getString("name");
                    String icon = jsonObject.getString("icon");
                    String theme = jsonObject.getString("theme");
                    String ranking = jsonObject.getString("ranking");
                    String state = jsonObject.getString("state");
                    seniorityList.add(new Seniority(id,name,icon,ranking,state, theme));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.e("BBBBB","==资讯界面ListView=="+seniorityList.toString());
        return seniorityList;
    }

    /*
    * 解析今日推荐
    * */
    public static List<Boutiques> parseBoutiquesJson(String jsonPath,int num){
        List<Boutiques> manchesterList = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(jsonPath);
            if (jsonObject!=null){
               switch (num){
                   case 1://今日推荐
                       JSONObject boutiques = jsonObject.getJSONObject("boutiques");
                       JSONArray boutique = boutiques.getJSONArray("boutique");
                       if (boutique!=null&&boutique.length()>0){
                           for (int i =0;i<boutique.length();i++){
                               JSONObject jsonObject1 = boutique.getJSONObject(i);
                               String id = jsonObject1.getString("id");
                               String name = jsonObject1.getString("name");
                               String icon = jsonObject1.getString("icon");
//                               Log.e("AAAA","==精品今日推荐name=="+name);
                               manchesterList.add(new Boutiques(id,name,icon));
                           }
                       }
                       break;
                   case 2://今日热门
                       JSONObject rankings = jsonObject.getJSONObject("rankings");
                       JSONArray ranking = rankings.getJSONArray("ranking");
                       if (ranking!=null&&ranking.length()>0){
                           for (int i =0;i<ranking.length();i++){
                               JSONObject jsonObject1 = ranking.getJSONObject(i);
                               String id = jsonObject1.getString("id");
                               String name = jsonObject1.getString("name");
                               String icon = jsonObject1.getString("icon");
                               manchesterList.add(new Boutiques(id,name,icon));
                           }
                       }
                       break;
                   case 3://今日最新
                       JSONObject news = jsonObject.getJSONObject("news");
                       JSONArray new1 = news.getJSONArray("new");
                       if (new1!=null&&new1.length()>0){
                           for (int i =0;i<new1.length();i++){
                               JSONObject jsonObject1 = new1.getJSONObject(i);
                               String id = jsonObject1.getString("id");
                               String name = jsonObject1.getString("name");
                               String icon = jsonObject1.getString("icon");
                               manchesterList.add(new Boutiques(id,name,icon));
                           }
                       }
                       break;
                   case 4://分类
                       JSONArray classification = jsonObject.getJSONArray("classification");
                       if (classification!=null&&classification.length()>0){
                            for (int i =0 ;i<classification.length();i++){
                                JSONObject jsonObject1 = classification.getJSONObject(i);
                                String id = jsonObject1.getString("id");
                                String name = jsonObject1.getString("name");
                                String icon = jsonObject1.getString("icon");
                                manchesterList.add(new Boutiques(id,name,icon));
                            }
                       }
                       break;
               }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return manchesterList;
    }

    /*
   * 解析资讯页面顶部广告数据
   * */
    public static List<HeadTitls> parseHeadJson(String jsonPath){
        List<HeadTitls> manchesterList = new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONArray(jsonPath);
            if (jsonArray!=null&&jsonArray.length()>0){
                for (int i =0;i<jsonArray.length();i++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                    String id = jsonObject1.getString("id");
                    String title = jsonObject1.getString("title");
                    String l_picture = jsonObject1.getString("l_picture");
                    manchesterList.add(new HeadTitls(id,title,l_picture));
                 }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return manchesterList;
    }

    public static List<HeadList> parseHeadList(String pathJson){
        List<HeadList> headLists = new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONArray(pathJson);
            if (jsonArray!=null&&jsonArray.length()>0){
                for (int i =0;i<jsonArray.length();i++){
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String id = jsonObject.getString("id");
                    String title = jsonObject.getString("title");
                    String  newsauthor_content = jsonObject.getString("newsauthor_content");
                    String newstype_content = jsonObject.getString("newstype_content");
                    String cover = jsonObject.getString("cover");
                    headLists.add(new HeadList(id,title,newsauthor_content, newstype_content, cover));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
//
        return headLists;
    }
    /*
    * 解析资讯界面的图片
    * */
    public static List<PassList> parsePicJson(String path){
       List<PassList> list = new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONArray(path);
            if (jsonArray!=null&&jsonArray.length()>0){
                for (int i =0;i<jsonArray.length();i++){
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                      String  picture = jsonObject.getString("picture");
                    list.add(new PassList(picture));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
//        Log.e("AAA","==图片list"+list.toString());
        return list;
    }
/*
* 热门搜索的解析
* */
    public static List<String> parseNameJson(String path){
        List<String> list = new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONArray(path);
            if (jsonArray!=null&&jsonArray.length()>0){
                for (int i =0;i<jsonArray.length();i++){
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String  picture = jsonObject.getString("name");
                    list.add(picture);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
//        Log.e("AAA","==图片list"+list.toString());
        return list;
    }

    /*
    * 解析资讯段子页面的数据
    * */

    public static List<TalkList> parseTalkJson(String path){
        List<TalkList> lists = new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONArray(path);
            if (jsonArray!=null&&jsonArray.length()>0){
                for (int i =0;i<jsonArray.length();i++){
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String id = jsonObject.getString("id");
                    String name = jsonObject.getString("name");
                    String imgurl = jsonObject.getString("imgurl");
                    String commend = jsonObject.getString("commend");
                    String createtime = jsonObject.getString("createtime");
                    String text = jsonObject.getString("text");
                    if (id!=null&&name!=null&&imgurl!=null&&commend!=null&&text!=null){
                        lists.add(new TalkList(id,name,imgurl,commend, createtime, text));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return lists;
    }

    /*
    * 精品界面的目录界面数据解析
    * */

    public static List<DetialList> parseDetialPath(String path){
        List<DetialList> lists = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(path);
            if (jsonObject!=null){
                String id = jsonObject.getString("id");
                String name = jsonObject.getString("name");
                String icon = jsonObject.getString("icon");
                String author = jsonObject.getString("author");
                String state = jsonObject.getString("state");
                String ranking = jsonObject.getString("ranking");
                String introduction = jsonObject.getString("introduction");
                JSONArray chapter = jsonObject.getJSONArray("chapter");
                if (chapter!=null&&chapter.length()>0){
                    for (int i =0;i<chapter.length();i++){
                        JSONObject jsonObject1 = chapter.getJSONObject(i);
                        String title = jsonObject1.getString("title");
                        String number = jsonObject1.getString("number");
                        lists.add(new DetialList(id,name, icon, author, state, introduction, title, number,ranking));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
//        Log.e("AAAA","==精品跳转目录页面=="+lists.toString());
        return lists;
    }

    /*
    * 解析读漫画的页面
    * */
    public static List<ReaderPicture> parseReaderPic(String path){
        List<ReaderPicture> pictures = new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONArray(path);
            if (jsonArray!=null&&jsonArray.length()>0){
                for (int i =0;i<jsonArray.length();i++){
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String icon = jsonObject.getString("icon");
                    String page = jsonObject.getString("page");
                    String title = jsonObject.getString("title");
                    pictures.add(new ReaderPicture(icon, page,title));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pictures;
    }

}
